package com.example.mybatisplus.controller;

import com.example.mybatisplus.model.domain.SingleTime;
import com.example.mybatisplus.model.domain.User;
import com.example.mybatisplus.service.SingleTimeService;
import com.example.mybatisplus.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.TeacherRotaService;
import com.example.mybatisplus.model.domain.TeacherRota;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2022-06-02
 * @version v1.0
 */
@RestController
@RequestMapping("/api/teacherRota")
public class TeacherRotaController {

    private final Logger logger = LoggerFactory.getLogger( TeacherRotaController.class );

    @Autowired
    private TeacherRotaService teacherRotaService;
    @Autowired
    private SingleTimeService singleTimeService;
    @Autowired
    private UserService userService;

    @GetMapping("")
    public JsonResponse show(){
        List<TeacherRota> teacherRota = teacherRotaService.list();
        List<SingleTime> singleTimes = singleTimeService.list();
        List<Map<Object,Object>> result = new ArrayList<>();
        for(TeacherRota t : teacherRota){
            Map<Object,Object> temp = new HashMap<>();
//            teaId:'',
//            timeId:'',
//            teacherType:'',
//            teaName:''
            temp.put("id",t.getId());
            temp.put("teaId",t.getTeaId());
            temp.put("timeId",t.getTimeId());
            temp.put("teacherType",t.getTeacherType());
            temp.put("teaName",t.getTeaName());
            for(SingleTime s : singleTimes){
                if(s.getId().toString().equals(t.getTimeId().toString())){
                    temp.put("time",s.getWeakDay()+" "+s.getTimeDay());
                }
            }
            result.add(temp);
        }
        return JsonResponse.success(result).setMessage("查询值班信息成功");
    }

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        TeacherRota  teacherRota =  teacherRotaService.getById(id);
        return JsonResponse.success(teacherRota);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @DeleteMapping( "/delete/{id}")
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        try {
            teacherRotaService.removeById(id);
            return JsonResponse.success(null).setMessage("删除值班信息成功");
        } catch (Exception e) {
            return JsonResponse.failure(e.getMessage());
        }
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateTeacherRota(TeacherRota  teacherRota) throws Exception {
        teacherRotaService.updateById(teacherRota);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建TeacherRota
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(@RequestBody TeacherRota  teacherRota) throws Exception {
        try{
            List<TeacherRota> teacherRotas = teacherRotaService.list();
            for(TeacherRota t : teacherRotas){
                if(teacherRota.getTeaId().toString().equals(t.getTeaId().toString())&&
                teacherRota.getTimeId().toString().equals(t.getTimeId().toString())){
                    throw new Exception("该咨询师在该时段已安排值班");
                }
            }
            return JsonResponse.success(teacherRotaService.save(teacherRota)).setMessage("添加值班信息成功");
        } catch (Exception e){
            return JsonResponse.failure("添加值班信息失败 "+e.getMessage());
        }
    }
}

